package com.example.letgo.trip;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.letgo.R;
import com.example.letgo.model.User;
import com.example.letgo.tool.CircleTransform;
import com.example.letgo.tool.ShareGridDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;
import com.tencent.connect.share.QzonePublish;
import com.tencent.connect.share.QzoneShare;
import com.tencent.open.utils.HttpUtils;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.provider.UserDictionary.Words.APP_ID;
import static com.tencent.connect.share.QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT;

/**
 * Created by cc on 2018/1/31.
 */

public class TripDetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView,head,trip_back;
    private WebView webView;
    private TextView textView_title,textView_describe,textView_content,textView_trip_departure,trip_date,
            trip_sex,trip_cost,user_number,trip_destination,user_name,trip_time;
    private ImageButton add_btn;

    private Tencent mTencent;
    private String destination1,describe1;

    private ArrayList<Map<String, Object>> mlist=new ArrayList<Map<String,Object>>();



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trip_details);
        mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext());



        webView = (WebView) findViewById(R.id.webview);
        add_btn = (ImageButton)findViewById(R.id.add);
        trip_back = findViewById(R.id.trip_back);
        //设置WebView支持JavaScript
        webView.getSettings().setJavaScriptEnabled(true);


        webView.setWebViewClient(new Client());
        webView.loadUrl("file:///android_asset/comment.html");

        head = findViewById(R.id.trip_user_head);
        user_name = findViewById(R.id.user_name);
        trip_time = findViewById(R.id.trip_publish_time);


        imageView = (ImageView) findViewById(R.id.trip_content_img);
        textView_title = (TextView) findViewById(R.id.trip_name);
        textView_describe = (TextView) findViewById(R.id.trip_introduce_text);
        textView_content = (TextView) findViewById(R.id.trip_content_text);
        trip_destination = (TextView) findViewById(R.id.trip_destination);
        textView_trip_departure = (TextView) findViewById(R.id.trip_departure);
        trip_date = (TextView) findViewById(R.id.trip_date);
        trip_sex = (TextView) findViewById(R.id.trip_sex);
        trip_cost = (TextView) findViewById(R.id.trip_cost);
        user_number = (TextView) findViewById(R.id.user_number);


//        获取 listview 传的值 并加载到 trip_deatails 页面之中

        Bundle bundle = getIntent().getExtras();
        String destination = bundle.getString("teamOverPlace");
        int userid = bundle.getInt("teamUserId");
        String content = bundle.getString("teamText");
        String img = bundle.getString("teamImage");
        String describe = bundle.getString("teamDescribe");
        String departure = bundle.getString("teamStartPlace");
        String date = bundle.getString("teamDate");
        String sex = bundle.getString("teamSex");
        String cost = bundle.getString("teamMoney");
        String number = bundle.getString("teamPersons");
        String teamTime = bundle.getString("teamTime");

        destination1 = bundle.getString("teamOverPlace");
        describe1 = bundle.getString("teamDescribe");

        textView_title.setText(destination);
        textView_content.setText(content);
        Picasso.with(this).load(img).into(imageView);

        textView_describe.setText(describe);
        trip_destination.setText(destination);
        textView_trip_departure.setText(departure);
        trip_date.setText(date);
        trip_sex.setText(sex);
        trip_cost.setText(cost);
        user_number.setText(number);
        trip_time.setText(teamTime);


        userinit(userid);

        trip_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 监听报名按钮
        add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView user_number = findViewById(R.id.user_number);
                Integer a = Integer.parseInt(user_number.getText().toString());
                if (a >0 ){
                    Integer b = a-1;
                    String c = Integer.toString(b);
                    user_number.setText(c);
                    Toast.makeText(TripDetailsActivity.this, "报名成功！", Toast.LENGTH_SHORT).show();

                    add_btn.setClickable(false);
                }else {
                    Toast.makeText(TripDetailsActivity.this, "名额已满！", Toast.LENGTH_SHORT).show();
                }

            }
        });

        findViewById(R.id.btn_share_qq).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_share_qq) {
            ShareGridDialog dialog = new ShareGridDialog(this, null);
            dialog.setUrl("https://blog.csdn.net/qq_37501744/article/details/79186366");
            dialog.setTitle(destination1);
            dialog.setContent(describe1);
            dialog.setImgUrl("");
            dialog.show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != mTencent) {
            mTencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener());
        }
    }


    private void userinit(final int userid){

        OkGo.get("http://119.29.113.114:8080/gogogo/user")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("s",s);
                        Gson gson = new Gson();
                        Type listType = new TypeToken<LinkedList<User>>(){}.getType();
                        LinkedList<User> list = gson.fromJson(s, listType);


                        for (int i = 0;i<list.size();i++){

                            if (userid==list.get(i).getUserId()){
                                Log.i("TAG","................ "+list.get(i).getUserId());
                                mlist.clear();
                                String ImageUrl = "http://119.29.113.114:8080/gogogo/"+list.get(i).getUserImg();
                                String nickname = list.get(i).getUserNickName();


                                Picasso.with(TripDetailsActivity.this).load(ImageUrl).transform(new CircleTransform()).into(head);
                                user_name.setText(nickname);
                            }
                        }
                    }
                });
    }

    class Client extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private class BaseUiListener implements IUiListener {

        protected void doComplete(JSONObject values) {

        }

        @Override
        public void onComplete(Object o) {
            Toast.makeText(TripDetailsActivity.this, "分享成功！", Toast.LENGTH_SHORT).show();
        }

        @Override

        public void onError(UiError e) {
            Toast.makeText(TripDetailsActivity.this, "分享失败！", Toast.LENGTH_SHORT).show();
        }

        @Override

        public void onCancel() {

        }

    }
}
